local sk__dianjie = fk.CreateSkill {

  name = "sk__dianjie",

  tags = {  },

}



sk__dianjie:addEffect(fk.EventPhaseChanging, {
  name = "sk__dianjie",
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__dianjie.name) and (data.phase == Player.Draw or data.phase == Player.Play)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__dianjie.name, nil, "#sk__dianjie"..(data.phase - 3).."-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = sk__dianjie.name,
      pattern = ".|.|spade,club",
    }
    room:judge(judge)
    if player.dead then return end
    if judge.card.suit == Card.Club then
      local targets = table.map(table.filter(room.alive_players, function(p)
        return not p.chained end), Util.IdMapper)
      if #targets == 0 then return end
      local tos = room:askForChoosePlayers(player, targets, 1, 999, "#sk__dianjie1-choose", sk__dianjie.name, true)
      if #tos > 0 then
        local tos_players= table.map(tos, function(id) return room:getPlayerById(id) end)
        room:sortByAction(tos_players)
        for _, id in ipairs(tos_players) do
          id:setChainState(true)
        end
      end
    elseif judge.card.suit == Card.Spade then
      local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
        "#sk__dianjie2-choose", sk__dianjie.name, true)
      if #to > 0 then
        room:damage{
          from = player,
          to = room:getPlayerById(to[1]),
          damage = 2,
          damageType = fk.ThunderDamage,
          skillName = sk__dianjie.name,
        }
      end
    end
    data.skipped = true
  end,
})

return sk__dianjie